package pkg

import (
	"fmt"
	"github.com/smartwalle/alipay/v3"
	"user-srv/basic/config"
)

func ALiPay(subject, outTradeNo, totalAmount string) (string, error) {
	var privateKey = config.Config.AliPay.PrivateKey
	var appId = config.Config.AliPay.AppId
	var client, err = alipay.New(appId, privateKey, false)
	if err != nil {
		fmt.Println(err)
		return "", err
	}

	var p = alipay.TradeWapPay{}
	p.NotifyURL = "http://7c8b07e9.r39.cpolar.top/notify/pay"
	p.ReturnURL = "https://www.baidu.com/"
	p.Subject = subject
	p.OutTradeNo = outTradeNo
	p.TotalAmount = totalAmount
	p.ProductCode = "QUICK_WAP_WAY"

	url, err := client.TradeWapPay(p)
	if err != nil {
		fmt.Println(err)
		return "", err
	}

	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	fmt.Println(payURL)
	return payURL, nil
}
